﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HQWeb.SysManage.BranchManage
{
    public partial class BranchEdit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                BindList();
                checkState.Checked = true;
            }
        }

        #region 绑定列表数据
        private void BindList()
        {

            BLL.SysManage.T_PUB_BRANCH bll = new BLL.SysManage.T_PUB_BRANCH();

            GridView1.DataSource = bll.GetModelList(""); ;
            GridView1.DataBind();
        }
        #endregion

        #region 翻页事件
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                //strWhere = strWhere.Replace("order by operatetime DESC", "");
                int newPageIndex = 0;

                if (-2 == e.NewPageIndex)
                { // when click the "GO" Button
                    TextBox txtNewPageIndex = null;
                    GridViewRow pagerRow = GridView1.Controls[0].Controls[GridView1.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
                    //GridViewRow pagerRow = GridView1.BottomPagerRow; //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
                    if (null != pagerRow)
                    {
                        txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value
                    }

                    if (null != txtNewPageIndex)
                    {
                        newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
                    }

                    newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
                    newPageIndex = newPageIndex >= GridView1.PageCount ? GridView1.PageCount - 1 : newPageIndex;
                    GridView1.PageIndex = newPageIndex;
                }
                else if (e.NewPageIndex == -1)
                {
                    newPageIndex = 0;
                    GridView1.PageIndex = 0;
                }
                else
                {  // when click the first, last, previous and next Button
                    newPageIndex = e.NewPageIndex;
                    GridView1.PageIndex = e.NewPageIndex;
                }

                // check to prevent form the NewPageIndex out of the range


                BindList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 编辑

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strCommand = e.CommandName;
            string strID = e.CommandArgument.ToString();
            if (strCommand.Equals("UpdateData"))  //编辑
            {
                BLL.SysManage.T_PUB_BRANCH bll = new BLL.SysManage.T_PUB_BRANCH();
                int temp;
                int.TryParse(strID, out temp);
                Model.SysManage.T_PUB_BRANCH model = bll.GetModel(temp);
                if (model != null)
                {
                    txtName.Text = model.BRANCH_NAME;
                    txtAddress.Text = model.ADDRESS;
                    txtIP.Text = model.NAS_IP;
                    txtNASUser.Text = model.NAS_USERID;
                    txtNASPwd.Text = model.NAS_PASSWORD;
                    txtPerson.Text = model.HEAD;
                    btnSubmit.CommandName = "edit";
                    btnSubmit.Text = "修 改";
                    btnSubmit.CommandArgument = strID;
                    checkState.Checked = model.IS_BRANCH;
                    txtPhone.Text = model.PHONE;
                    labTitle.Text = "修改店信息";
                    checkSynchronize.Checked = model.IS_SYNCHRONIZED;
                }
            }
            //  BindList();
        }


        #endregion

        #region 按钮事件

        //提交按钮
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string strCommand = btnSubmit.CommandName;
            BLL.SysManage.T_PUB_BRANCH bll = new BLL.SysManage.T_PUB_BRANCH();
            Model.SysManage.T_PUB_BRANCH model = null;
            if (strCommand.Equals("edit"))
            {
                string strID = btnSubmit.CommandArgument.ToString();
                int temp;
                int.TryParse(strID, out temp);
                model = bll.GetModel(temp);
            }
            if (model == null)
            {
                model = new Model.SysManage.T_PUB_BRANCH();
            }
            model.HEAD = txtPerson.Text.Trim();
            model.NAS_IP = txtIP.Text.Trim();
            model.NAS_USERID = txtNASUser.Text.Trim();
            model.NAS_PASSWORD = txtNASPwd.Text.Trim();
            model.BRANCH_NAME = txtName.Text.Trim();
            model.ADDRESS = txtAddress.Text.Trim();
            model.IS_BRANCH = checkState.Checked;
            model.IS_SYNCHRONIZED = checkSynchronize.Checked;
            model.PHONE = txtPhone.Text.Trim();
            if (strCommand.Equals("edit"))
            {
                bll.Update(model);
            }
            else
            {
                bll.Add(model);
            }
            btnCancle_Click(new object(), new EventArgs());
            BindList();
        }

        //取消
        protected void btnCancle_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtAddress.Text = "";
            txtIP.Text = "";
            txtNASPwd.Text = "";
            txtNASUser.Text = "";
            txtPhone.Text = "";
            txtPerson.Text = "";
            btnSubmit.CommandName = "add";
            checkState.Checked = true;
            checkSynchronize.Checked = true;
            btnSubmit.Text = "添 加";
            labTitle.Text = "添加店信息";

        }

        #endregion

    }
}